public with sharing class MC_SignUpExtension
{
	private final Missional_Community_Member__c ControllerMember;

	public Contact NewContact { get; set; }
	public Missional_Community__c ProvidedGroup
	{
		get
		{
			if(ProvidedGroup==null)
			{
				String groupId=ApexPages.currentPage().getParameters().get('group');
				if(groupId==null||groupId=='')
				{
					System.debug('group was null');
					return null;
				}			
				ProvidedGroup=[select id, group_name__c, leader_name__c, meeting_day__c from missional_community__c where id=:groupId];
			}
			return ProvidedGroup;
		}
		
		set;
	}

    public MC_SignUpExtension(ApexPages.StandardController controller)
    {
		ControllerMember=(Missional_Community_Member__c) controller.getRecord();
		
		NewContact=new Contact();
    }
    
	public System.PageReference save()
	{
		//Find Account for contact
		//XYZZY replace this with a setting or do accounts right
		Account MCMembers=[select id from Account where Name='Missional Community Members' limit 1];
		NewContact.AccountId=MCMembers.Id;
		//Save Contact
		insert NewContact;
		ControllerMember.Contact__c=NewContact.Id;
		//Lookup Group by query param
		ControllerMember.Missional_Community__c=ProvidedGroup.Id;		//XYZZY add error handling for no provided group
		//Save member
		ControllerMember.date_joined__c=DateTime.now();
		insert ControllerMember;	//Note, no upsert here. We only add new members from this control
		
		return Page.MC_JoinSuccess;
	}
}